package com.example.springstudy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springstudy.dao.res.ShopListRes;
import com.example.springstudy.entity.Shop;
import com.example.springstudy.mapper.ShopMapper;
import com.example.springstudy.service.ShopService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {

    @Override
    public ShopListRes getShopList(int page, int size) {
        LambdaQueryWrapper<Shop> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Shop::getId).eq(Shop::getIsDeleted, 0);
        int total = (int)this.count(wrapper);
        List<Shop> list = this.list(new Page<Shop>(page, size), wrapper);
        return new ShopListRes(list, total, page, size);
    }

    @Override
    public Shop saveShop(Shop shop) {
        return this.save(shop)?shop:null;
    }

    @Override
    public Shop updateShop(Shop shop) {
        return this.updateById(shop)?shop:null;
    }

    @Override
    public boolean deleteShop(Shop shop) {
        shop.setIsDeleted(1);
        return this.updateById(shop);
    }
}
